#pragma once
#include<vector>


namespace usr
{
	template<class T>
	class less
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x < y;
		}
	};

	template<class T>
	class greater
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x > y;
		}
	};

	template<class T, class Container = std::vector<T>,class Compare=greater<T>>
	class priority_queue
	{
	private:
		Container con;
		Compare compare;

		void adjust_up(int index)
		{
			/*while (con[index] > con[(index - 1) / 2])
			{
				std::swap(con[index], con[(index - 1) / 2]);
				index = (index - 1) / 2;
			}*/

			while (compare(con[index], con[(index - 1) / 2]))
			{
				std::swap(con[index], con[(index - 1) / 2]);
				index = (index - 1) / 2;
			}
		}

		void adjsut_down(int index)
		{
			int left = index * 2 + 1;
			/*while (left < size())
			{
				int max = left + 1 < size() && con[left + 1] > con[left] ? left + 1 : left;
				if (con[max] > con[index])
				{
					std::swap(con[max], con[index]);
					index = max;
					left = index * 2 + 1;
				}
				else
				{
					break;
				}
			}*/
			
			while (left < size())
			{
				int max = left + 1 < size() && compare(con[left + 1], con[left]) ? left + 1 : left;
				if (compare(con[max], con[index]))
				{
					std::swap(con[max], con[index]);
					index = max;
					left = index * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}
	public:
		void push(const T& x)
		{
			con.push_back(x);
			adjust_up(con.size() - 1);
		}

		void pop()
		{
			std::swap(con[0], con[size() - 1]);
			con.pop_back();
			adjsut_down(0);
		}

		size_t size()
		{
			return con.size();
		}

		const T& front()
		{
			return con.front();
		}

		const T& back()
		{
			return con.back();
		}
		bool empty()
		{
			return con.empty();
		}
	};
}
